/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.beActionAssembler;

import com.inspur.edp.bef.bizentity.operation.BizAction;
import com.inspur.edp.bef.dtgenerator.core.BefCoreGenCtx;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.generator.overall.Utils;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;

//
//        public override bool  demandDataPermission(IEntityData data)
//        {
//            var authCtxList = new List<DataPermissionContext>()
//            {
//                new DataPermissionContext
//                {
//                    AuthObjId = "",
//                    AuthOp = "",
//                    AuthorizationId = "",
//                    Element = ""
//                },
//                new DataPermissionContext
//                {
//                    AuthObjId = "",
//                    AuthOp = "",
//                    AuthorizationId = "",
//                    Element = ""
//                }
//            };
//            return AuthUtil.DemandDataPermission(data, authCtxList);
//        }
//
public class BEAssDemandPermissionGenerator extends ClassMethodGenerator {
  public static final String ItemName = "DemandDataPermission";
  private static final String formalParName_data = "data";
  private BefCoreGenCtx coreGenCtx;
  private BizAction action;

  public BEAssDemandPermissionGenerator(BefCoreGenCtx coreGenCtx, BizAction action) {
    this.coreGenCtx = coreGenCtx;
    this.action = action;
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>(
        java.util.Arrays.asList(ModifierKeyword.PUBLIC_KEYWORD));
  }

  @Override
  protected String getMethodName() {
    return ItemName;
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    java.util.ArrayList<ParameterInfo> tempVar = new java.util.ArrayList<ParameterInfo>(1);
    ParameterInfo p = new ParameterInfo();
    p.setParamName(formalParName_data);
    p.setParamType(Utils.createTypeInfo(IEntityData.class));
    tempVar.add(p);
    return tempVar;
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo("bool");
  }

  @Override
  protected Block buildMethodBody() {
    // TODO: java版临时注释 权限暂不提供
    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newBooleanLiteral(true));
    block.statements().add(returnStatement);
    return block;
    //		java.util.ArrayList<SyntaxNodeOrToken> authCtxLists = new
    // java.util.ArrayList<SyntaxNodeOrToken>();
    //		for (var authObjInfo : coreGenCtx.getBeGenCtx().getBE().getMainObject().AuthFieldInfos)
    //		{
    //			if (authObjInfo.ActionInfos.size() == 0 || !authObjInfo.ActionInfos.TryGetValue(action.ID,
    // out var actionInfo))
    //			{
    //				continue;
    //			}
    //			if (authCtxLists.Any())
    //			{
    //				authCtxLists.add(SyntaxFactory.Token(SyntaxKind.CommaToken));
    //			}
    //
    //	authCtxLists.add(SyntaxFactory.ObjectCreationExpression(TypeUtils.GetTypeSyntax(DataPermissionContext.class)).WithInitializer(SyntaxFactory.InitializerExpression(SyntaxKind.ObjectInitializerExpression, SyntaxFactory.<ExpressionSyntax>SeparatedList(new SyntaxNodeOrToken[]{ SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName("AuthFieldId"), SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(authObjInfo.AuthFieldID))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName("AuthOp"), SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(actionInfo.OperationID))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName("AuthorizationId"), SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(authObjInfo.AuthorizationID))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName("Element"), SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(coreGenCtx.getBeGenCtx().getBE().getMainObject().FindElement(authObjInfo.ElementID).LabelID)))}))));
    //		}
    //		if (!authCtxLists.Any())
    //		{
    //			return
    // SyntaxFactory.Block(SyntaxFactory.<StatementSyntax>SingletonList(SyntaxFactory.ReturnStatement(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression))));
    //		}
    //		return
    // SyntaxFactory.Block(SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var")).WithVariables(SyntaxFactory.<VariableDeclaratorSyntax>SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier("authCtxList")).WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.ObjectCreationExpression(TypeUtils.GetTypeSyntax(java.util.ArrayList<DataPermissionContext>.class)).WithArgumentList(SyntaxFactory.ArgumentList()).WithInitializer(SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, SyntaxFactory.<ExpressionSyntax>SeparatedList(authCtxLists.toArray(new SyntaxNodeOrToken[]{}))))))))), SyntaxFactory.ReturnStatement(SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, TypeUtils.GetTypeSyntax(AuthUtil.class), SyntaxFactory.IdentifierName("DemandDataPermission"))).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SeparatedList(new SyntaxNodeOrToken[]{ SyntaxFactory.Argument(SyntaxFactory.IdentifierName(formalParName_data)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument(SyntaxFactory.IdentifierName("authCtxList"))})))));
  }
}
